package com.lqk.springbootminio.controller;

import com.lqk.springbootminio.config.MinioTemplate;
import com.lqk.springbootminio.properties.MinioConfigProperties;
import com.lqk.springbootminio.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author liqiankun@nnuo.com
 * @date 2023/3/24 10:45
 * @description
 **/
@Api(tags = "minio测试")
@Slf4j
@RestController
@RequestMapping("/oss/file")
public class OssController {
    @Autowired
    private MinioTemplate minioTemplate;
    @Autowired
    private MinioConfigProperties properties;
    @Autowired
    private OssService ossService;

    /**
     * 文件上传
     *
     * @param file 文件
     */
    @ApiOperation(value = "文件上传", notes = "文件上传")
    @PostMapping("/upload")
    public String upload(@RequestPart("file") MultipartFile file) {
        return ossService.upload(file);
    }

    /**
     * 删除
     *
     * @param fileName 文件名
     */
    @ApiOperation(value = "删除", notes = "删除")
    @DeleteMapping("/delete")
    public void delete(@RequestParam("fileName") String fileName) {
        ossService.removeFile(fileName);
    }

    /**
     * 获取文件信息
     *
     * @param fileName 文件名
     * @return 文件信息
     */
    @ApiOperation(value = "获取文件信息", notes = "获取文件信息")
    @GetMapping("/info")
    public String getFileStatusInfo(@RequestParam("fileName") String fileName) {
        return ossService.getFileStatusInfo(fileName);
    }

    /**
     * 获取文件外链
     *
     * @param fileName 文件名
     * @return 文件外链
     */
    @ApiOperation(value = "获取文件外链", notes = "获取文件外链")
    @GetMapping("/url")
    public String getPresignedObjectUrl(@RequestParam("fileName") String fileName) {
        return ossService.getPresignedObjectUrl(fileName);
    }

    /**
     * 文件下载
     *
     * @param fileName 文件名
     */
    @ApiOperation(value = "文件下载", notes = "文件下载")
    @GetMapping("/download")
    public void download(@RequestParam("fileName") String fileName) {
        ossService.download(fileName);
    }
}
